package edu.ssdut.revise.models;

import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.body.MethodDeclaration;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public class VisMethodNode extends VisBaseNode {
    private final List<String> annotations;
    private final String identifier;
    private final List<String> modifiers;
    private final String body;
    private final List<VisParamNode> parameters;
    private final String type;

    /**
     * construct with MethodDeclaration
     *
     * @param md methodDeclaration
     */
    public VisMethodNode(MethodDeclaration md) {
        super(md);
        // TODO: methods can also have inner classes, consider extracting an interface to provide the ability to have inner classes;
        List<String> modifiers = md.getModifiers().stream().map(Node::toString).collect(Collectors.toList());

        this.identifier = md.getNameAsString();
        this.modifiers = modifiers;

        String bodyString;
        bodyString = "";
        if (md.getBody().isPresent()) {
            bodyString = md.getBody().get().toString();
        }
        this.body = bodyString;
        this.annotations = md.getAnnotations().stream().map(Objects::toString).collect(Collectors.toList());
        this.parameters = md.getParameters().stream().map(VisParamNode::new).collect(Collectors.toList());
        this.type = md.getType().toString();
    }

    @Override
    public String toString() {
        return "Method identifier: " + this.identifier + System.lineSeparator() +
                "  " + "method modifiers: " + this.modifiers.toString() + System.lineSeparator() +
                "  " + "returning: " + this.type +
                "  " + "annotations: " + annotations.toString() + System.lineSeparator() +
                "  " + "params: " + parameters.toString() + System.lineSeparator() +
                "  " + "method body: " + System.lineSeparator() +
                this.body + System.lineSeparator();
    }

    @Override
    public String getTypeName() {
        return "MethodNode";
    }

    public List<String> getAnnotations() {
        return annotations;
    }

    public String getIdentifier() {
        return identifier;
    }

    public List<String> getModifiers() {
        return modifiers;
    }

    public String getBody() {
        return body;
    }

    public String getType() {
        return type;
    }

    public List<VisParamNode> getParameters() {
        return parameters;
    }
}
